from common.config import *
import requests
import allure
from common.log_info import Logger
from common.pyredis import *
from data.redis_data import *
import pytest

mini_log = Logger("minimall", "minimall").logger


@pytest.mark.skipif(pytest.global_env != 'test', reason="只在测试环境运行")
class MiniMall:

    def get_token(self):
        data = PyRedis().get_value(minimall.get("DB"), minimall.get("minimall_key"), minimall.get("minimall_value"))
        return data.get("token")

    @allure.testcase("加入心愿")
    def activity_add_wish(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + "flashmall-retail-minimall-server/v1/activity/addWish"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"加入心愿的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("移除心愿")
    def activity_remove_wish(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + "flashmall-retail-minimall-server/v1/activity/removeWish"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"移除心愿的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("获取心愿记录")
    def activity_get_wish(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + "flashmall-retail-minimall-server/v1/activity/getWish"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"获取心愿请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("商品加入购物车")
    def shoppingcart_add_goods(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + "flashmall-retail-minimall-server/v1/shoppingCart/add"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"商品加入购物车请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("商品移除购物车")
    def shoppingcart_delete_goods(self, env, mini_token, shoppingcart_id):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/shoppingCart/delete/{shoppingcart_id}"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"商品移除购物车请求参数：{shoppingcart_id}")
        return requests.post(url, headers=headers)

    @allure.testcase("获取购物车商品数量")
    def shoppingcart_get_count(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/shoppingCart/getCount"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"获取购物车商品数量请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("获取购物车列表数量")
    def shoppingcart_get_list(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/shoppingCart/getList"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"获取购物车列表请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("获取购物车支付金额数量")
    def shoppingcart_get_pay_amount(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/shoppingCart/getPayAmount"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"获取购物车支付金额请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("清空购物车数量")
    def shoppingcart_truncate(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/shoppingCart/truncate"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"清空购物车请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("修改购物车商品规格-数量")
    def shoppingcart_update(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/shoppingCart/update"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"修改购物车商品规格-数量请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("获取首页banner,金刚位")
    def search_banner_dazzling_merge_info(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/index/searchBannerDazzlingMergeInfo"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"获取首页banner,金刚位请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("获取购物车，支付后后页面商品推荐信息")
    def get_pay_page_goods_config_by_page_list(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/index/getPayPageGoodsConfigByPageList"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"获取商品推荐信息的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("根据集合id或分类id获取商品列表")
    def get_refspu_list(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/index/getRefSpuList"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"请求参数id：{data}")
        return requests.post(url, headers=headers, json=data)

    @allure.testcase("栏目的首页推荐信息的title信息")
    def get_index_recommend_title_list(self, env, mini_token, data):
        base_url = getconf(env, "qwc_url")
        url = base_url + f"flashmall-retail-minimall-server/v1/index/getIndexRecommendTitleList"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": mini_token}
        mini_log.info(f"获取title信息的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)
